{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The calculation of reciprocal basis for non-orthogonal basis in GAlgebra deviate from standard definition and caused a bug when calculating inverse metric.\n",
    "\n",
    "By definition, \n",
    "\n",
    "$$e_{i} \\cdot e^{j}=\\delta_{i}^{j}$$\n",
    "\n",
    "where $\\delta_{k}^{j}$ is the kronecker delta.\n",
    "\n",
    "But GAlgebra, intentionally makes it:\n",
    "\n",
    "$$e_{i} \\cdot e^{j}={E_n}^{2}\\delta_{i}^{j}$$\n",
    "\n",
    "where\n",
    "\n",
    "$$ E_{n} = e_{1}\\wedge \\dots \\wedge e_{n} $$\n",
    "\n",
    "In the docstring of `Ga.build_reciprocal_basis()`, it explicitly states:\n",
    "\n",
    "> For non-orthogonal basis $e^{j}$ is not normalized and must be divided by ${E_n}^{2}$ (`self.e_sq`) in any relevant calculations.\n",
    "\n",
    "The implementation of `Ga.build_reciprocal_basis()` claims to use formula 4.94 from GA4P by Doran and Lasenby:\n",
    "\n",
    "$$e^{i}=(-1)^{i-1}\\left(e_{1} \\wedge \\ldots \\wedge \\breve{e}_{i} \\wedge \\ldots \\wedge e_{n}\\right) E_{n}^{-1}$$\n",
    "\n",
    "But it looks like this:\n",
    "\n",
    "```python\n",
    "# Take all (n-1)-blades\n",
    "duals = list(self.blades_lst[-(self.n + 1):-1])\n",
    "# After reverse, the j-th of them is exactly e_{1}^...e_{j-1}^e_{j+1}^...^e_{n}\n",
    "duals.reverse()\n",
    "\n",
    "sgn = 1\n",
    "self.r_basis = []\n",
    "for dual in duals:\n",
    "    dual_base_rep = self.blade_to_base_rep(dual)\n",
    "    # {E_n}^{-1} = \\frac{E_n}{{E_n}^{2}}\n",
    "    # r_basis_j = sgn * duals[j] * E_n so it's not normalized, missing a factor of {E_n}^{-2}\n",
    "    r_basis_j = collect(expand(self.base_to_blade_rep(self.mul(sgn * dual_base_rep, self.e_obj))), self.blades_lst)\n",
    "    self.r_basis.append(r_basis_j)\n",
    "    # sgn = (-1)**{j-1}\n",
    "    sgn = -sgn\n",
    "```\n",
    "\n",
    "Let's verify this logic step by step using Kerr-Debney Metric:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from galgebra.printer import Format, GaLatexPrinter\n",
    "\n",
    "Format()\n",
    "from galgebra.ga import Ga\n",
    "from galgebra.mv import ONE, ZERO, HALF\n",
    "\n",
    "W = symbols(\"W\")\n",
    "c = symbols(\"c\")\n",
    "\n",
    "g4coords = (u, x, y, z) = symbols(\"u x y z\")\n",
    "g = [\n",
    "    [0, 0, -exp(-z), 0],\n",
    "    [0, HALF * u ** 2 * exp(4 * z), 0, 0],\n",
    "    [-exp(-z), 0, 12 * exp(-2 * z), u * exp(-z)],\n",
    "    [0, 0, u * exp(-z), HALF * u ** 2],\n",
    "]\n",
    "\n",
    "g4 = Ga(\"e_u e_x e_y e_z\", g=g, coords=g4coords, norm=False)  # Create g4\n",
    "(e_u, e_x, e_y, e_z) = g4.mv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display, Math\n",
    "\n",
    "def show(x):\n",
    "    display(Math(GaLatexPrinter.latex(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' \\\\left [ \\\\begin{array}{cccc} 0 & 0 & - e^{- z} & 0  \\\\\\\\ 0 & \\\\frac{u^{2} e^{4 z}}{2} & 0 & 0  \\\\\\\\ - e^{- z} & 0 & 12 e^{- 2 z} & u e^{- z}  \\\\\\\\ 0 & 0 & u e^{- z} & \\\\frac{u^{2}}{2}  \\\\end{array}\\\\right ] '"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}0 & 0 & - e^{- z} & 0\\\\0 & \\frac{u^{2} e^{4 z}}{2} & 0 & 0\\\\- e^{- z} & 0 & 12 e^{- 2 z} & u e^{- z}\\\\0 & 0 & u e^{- z} & \\frac{u^{2}}{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "None"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left [ e_{u}, \\quad e_{x}, \\quad e_{y}, \\quad e_{z}\\right ]$$"
      ],
      "text/plain": [
       "[eᵤ, eₓ, e_y, e_z]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.basis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{x}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for blade in g4.blades_lst:\n",
    "    show(blade)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Take all (n-1)-blades:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "duals = list(g4.blades_lst[-(g4.n + 1):-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for dual in duals:\n",
    "    show(dual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After reverse, the i-th of them is exactly $e_{1} \\wedge \\ldots \\wedge \\breve{e}_{i} \\wedge \\ldots \\wedge e_{n}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "duals.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for dual in duals:\n",
    "    show(dual)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Turn them into base reprsentation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - u e^{- z} \\boldsymbol{e}_{x} + \\boldsymbol{e}_{x}\\boldsymbol{e}_{y}\\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - u e^{- z} \\boldsymbol{e}_{u} + \\boldsymbol{e}_{u}\\boldsymbol{e}_{y}\\boldsymbol{e}_{z} + e^{- z} \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\boldsymbol{e}_{x}\\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\boldsymbol{e}_{x}\\boldsymbol{e}_{y} - e^{- z} \\boldsymbol{e}_{x}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for dual in duals:\n",
    "    show(g4.blade_to_base_rep(dual))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "show(g4.e_obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - (- u e^{- z} \\boldsymbol{e}_{x} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} + \\boldsymbol{e}_{x}\\boldsymbol{e}_{y}\\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z})$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - u e^{- z} \\boldsymbol{e}_{u} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} + \\boldsymbol{e}_{u}\\boldsymbol{e}_{y}\\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} + e^{- z} \\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\boldsymbol{e}_{u}\\boldsymbol{e}_{x}\\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\boldsymbol{e}_{x}\\boldsymbol{e}_{y} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} - e^{- z} \\boldsymbol{e}_{x} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i,dual in enumerate(duals):\n",
    "    show(S(-1)**(i-1)*g4.base_to_blade_rep(g4.blade_to_base_rep(dual) * g4.e_obj))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle u e^{- z} \\boldsymbol{e}_{x} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} - \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\boldsymbol{e}_{x}\\boldsymbol{e}_{y}\\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - u e^{- z} \\boldsymbol{e}_{u} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} + \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\boldsymbol{e}_{y}\\boldsymbol{e}_{z} + e^{- z} \\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\boldsymbol{e}_{u}\\boldsymbol{e}_{x}\\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\boldsymbol{e}_{u}\\boldsymbol{e}_{x}\\boldsymbol{e}_{y} - e^{- z} \\boldsymbol{e}_{x} \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i,dual in enumerate(duals):\n",
    "    show(collect(expand(S(-1)**(i-1)*g4.base_to_blade_rep(g4.blade_to_base_rep(dual) * g4.e_obj)),g4.blades_lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So it turns out the formula used is actually\n",
    "\n",
    "$$e^{i}=(-1)^{i-1}\\left(e_{1} \\wedge \\ldots \\wedge \\breve{e}_{i} \\wedge \\ldots \\wedge e_{n}\\right) E_{n}$$\n",
    "\n",
    "it's different from \n",
    "\n",
    "$$e^{i}=(-1)^{i-1}\\left(e_{1} \\wedge \\ldots \\wedge \\breve{e}_{i} \\wedge \\ldots \\wedge e_{n}\\right) E_{n}^{-1}$$\n",
    "\n",
    "by a factor of $E_{n}^{-2}$ which is a scalar.\n",
    "\n",
    "$E_n$ is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}  \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       " \\boldsymbol{e}_{u}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$E_n^2$ is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} - \\frac{u^{4} e^{2 z}}{4} \\end{equation*}"
      ],
      "text/plain": [
       "- \\frac{u^{4} e^{2 z}}{4}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.e * g4.e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$- \\frac{u^{4} e^{2 z}}{4}$$"
      ],
      "text/plain": [
       "  4  2⋅z \n",
       "-u ⋅ℯ    \n",
       "─────────\n",
       "    4    "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.e_sq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$E_n^{-2}$ is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$- \\frac{4 e^{- 2 z}}{u^{4}}$$"
      ],
      "text/plain": [
       "    -2⋅z \n",
       "-4⋅ℯ     \n",
       "─────────\n",
       "     4   \n",
       "    u    "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / g4.e_sq"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's clear that $$e_{i} \\cdot e^{j}={E_n}^{2}\\delta_{i}^{j}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{u^{4} e^{2 z}}{4}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{u^{4} e^{2 z}}{4}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{u^{4} e^{2 z}}{4}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{u^{4} e^{2 z}}{4}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i,base in enumerate(g4.basis):\n",
    "    show(g4.dot(g4.basis[i], g4.r_basis[i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And `g_inv` is implemented like this:\n",
    "\n",
    "```python\n",
    "        # Calculate inverse of metric tensor, g^{ij}\n",
    "\n",
    "        for i in self.n_range:\n",
    "            rx_i = self.r_symbols[i]\n",
    "            for j in self.n_range:\n",
    "                rx_j = self.r_symbols[j]\n",
    "                if j >= i:\n",
    "                    g_inv[i, j] = self.dot(self.r_basis_dict[rx_i], self.r_basis_dict[rx_j])\n",
    "                    if not self.is_ortho:\n",
    "                        g_inv[i, j] /= self.e_sq\n",
    "                else:\n",
    "                    g_inv[i, j] = g_inv[j, i]\n",
    "```\n",
    "\n",
    "which divide the dot product by $E_n^2$ and it's not enough and caused:\n",
    "\n",
    "$$g^{il} g_{lk} \\neq \\delta^i_k $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' \\\\left [ \\\\begin{array}{cccc} - \\\\frac{u^{4} e^{2 z}}{4} & 0 & 0 & 0  \\\\\\\\ 0 & - \\\\frac{u^{4} e^{2 z}}{4} & 0 & 0  \\\\\\\\ 0 & 0 & - \\\\frac{u^{4} e^{2 z}}{4} & 0  \\\\\\\\ 0 & 0 & 0 & - \\\\frac{u^{4} e^{2 z}}{4}  \\\\end{array}\\\\right ] '"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}- \\frac{u^{4} e^{2 z}}{4} & 0 & 0 & 0\\\\0 & - \\frac{u^{4} e^{2 z}}{4} & 0 & 0\\\\0 & 0 & - \\frac{u^{4} e^{2 z}}{4} & 0\\\\0 & 0 & 0 & - \\frac{u^{4} e^{2 z}}{4}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "None"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.g*g4.g_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The reason is that `g_inv` can be seen as:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "g_inv = eye(g4.n)\n",
    "g4.dot_mode = '|'\n",
    "for i in g4.n_range:\n",
    "    rx_i = g4.r_symbols[i]\n",
    "    for j in g4.n_range:\n",
    "        rx_j = g4.r_symbols[j]\n",
    "        if j >= i:\n",
    "            if g4.is_ortho:\n",
    "                g_inv[i, j] = g4.dot(g4.r_basis_dict[rx_i], g4.r_basis_dict[rx_j])\n",
    "            else:\n",
    "                # NOTE: both reciprocal basis vectors should be devided by E_n^2\n",
    "                g_inv[i, j] = g4.dot(g4.r_basis_dict[rx_i] / g4.e_sq, g4.r_basis_dict[rx_j] / g4.e_sq)\n",
    "        else:\n",
    "            g_inv[i, j] = g_inv[j, i]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now `g_inv` is correct and $$g^{il} g_{lk} = \\delta^i_k $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' \\\\left [ \\\\begin{array}{cccc} 1 & 0 & 0 & 0  \\\\\\\\ 0 & 1 & 0 & 0  \\\\\\\\ 0 & 0 & 1 & 0  \\\\\\\\ 0 & 0 & 0 & 1  \\\\end{array}\\\\right ] '"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & 0 & 0\\\\0 & 1 & 0 & 0\\\\0 & 0 & 1 & 0\\\\0 & 0 & 0 & 1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "None"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.g*g_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
